<?php

    /**
     * @package      Modules
     * @subpackage   Widgets_Classes_Db
     *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
    class DbWidgetParam extends BaseDbWidgetParam
    {

    ////////////////////////////////////////////////////////////////////////////
    //                                Methods                                 //
    ////////////////////////////////////////////////////////////////////////////

        /**
         * Retrieves parameters for given widget and administrator.
         *
         * @param       DbWidget    $widget
         * @param       DbAdmin     $admin
         * @return      array
         */
        public static function getValues(DbWidget $widget, DbAdmin $admin)
        {
            $values         = array();
            $params         = Doctrine_Query::create()
                                    ->from('DbWidgetParam wp')
                                    ->where('wp.status = ?', DbBase::ENABLED)
                                    ->addWhere('wp.widget_id = ?', $widget->id)
                                    ->execute();

            foreach ($params as $param) {
                $values[$param->param_key] = $param->getValue($admin);
            }

            return $values;
        }

        /**
         * Retrieves parameter value
         *
         * @param       DbAdmin     $admin
         * @return      mixed
         */
        public function getValue(DbAdmin $admin)
        {
            $adminParam         = Doctrine_Query::create()
                                    ->from('DbAdminWidgetParam awp')
                                    ->where('awp.admin_id = ?', $admin->id)
                                    ->addWhere('awp.param_id = ?', $this->id)
                                    ->fetchOne();

            $value = ($adminParam) ? $adminParam->param_value : $this->default_value;

            switch ($this->param_type) {
                case 'number':
                    $value = floatval($value);
                    break;
                case 'boolean':
                    $value = ($value == 'true' || $value == 1) ? true : false;
                    break;
            }

            return $value;
        }

        /**
         * Sets parameter value
         *
         * @param       DbAdmin     $admin
         * @param       mixed       $value
         * @return      mixed
         */
        public function setValue(DbAdmin $admin, $value)
        {
            $adminParam    = Doctrine_Query::create()
                                ->from('DbAdminWidgetParam awp')
                                ->where('awp.admin_id = ?', $admin->id)
                                ->addWhere('awp.param_id = ?', $this->id)
                                ->fetchOne();

            if ($adminParam) {
                $adminParam->param_value = $value;
                $adminParam->save();

            } else {
                $adminParam                     = new DbAdminWidgetParam();
                $adminParam->status             = DbBase::ENABLED;
                $adminParam->admin_id           = $admin->id;
                $adminParam->param_id           = $this->id;
                $adminParam->param_value        = $value;
                $adminParam->save();
            }
        }
    }